/***************************
统计分析-考核汇总
* */
<template>
  <basic-container>
    <div class="boxsty zhiBiao" v-if="zhiShow == 1">
    <el-row>
      <assessMentHead :sign="{ a: true, b: false, c: false,d: false,e: false}" ></assessMentHead>
    </el-row>
    <el-row style="margin:25px 0 0 0 ;">
      <div :height="searchheight">
        <span style="float:right;margin-top: -30px;cursor: pointer;" @click="searchchangeadvanced">
          {{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
          <i
            :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
          />
        </span>
        <div v-show="searchadvanced">
          <el-form :inline="true" :model="searchForm" class="form-inline">

            <el-form-item label="统计名称">
             <!-- <el-input v-model="searchForm.tjmc" placeholder="统计名称" :clearable="true"></el-input>-->
              <selectSearch @getDataByTjmc="getDataByTjmc"></selectSearch>
            </el-form-item>

            <el-form-item>
              <el-button
                icon="el-icon-search"
                type="primary"
                class="marginLeft"
                @click="getDataList(1)"
              >检索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-row>
    <el-row>
      <div style="height: 50px; border-bottom: 1px solid #b0bde2">
        <el-tooltip
          class="item"
          effect="dark"
          :content="contentName"
          placement="top"
        >
          <el-button
            icon="el-icon-plus"
            type="primary"
            circle
            @click="addOrEdit()"
          ></el-button>
        </el-tooltip>
        <el-tooltip
          class="item"
          effect="dark"
          content="批量删除选中的数据"
          placement="top"
        >
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="canDelete"
            @click="deleteBatch()"
            circle
          ></el-button>
        </el-tooltip>
        <div class="pull-right">
          <el-button plain type="primary" @click="download">导出</el-button>

        </div>
      </div>

    </el-row>
    <el-row>
      <el-table
        style="width: 100%;margin-top:20px"
        :data="dataList"
        border
        stripe
        header-align="center"
        v-if="rednermsg"
        ref="tableRef"
        v-loading="dataListLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" :index="indexMethod"  header-align="center" align="center" label="序号" width="55"></el-table-column>
        <el-table-column prop="tjmc" align="center" label="统计名称" show-overflow-tooltip >
          <template slot-scope="scope">
                <span
                  @click="addOrEdit(scope.row)"
                  style="color:rgb(14, 37, 140);cursor:pointer"
                >{{scope.row.tjmc}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="bz" align="center" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" align="center" min-width="120" label="创建时间"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="iskq" align="center" label="启动" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch
              active-value="1"
              inactive-value="0"
              v-model="scope.row.iskq"
              active-color="#17499a"
              inactive-color="#999999"
              @change="workStart(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" width="240" fixed="right" label="操作">

          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="small"
              @click="dataView(scope.row)"
            >汇总查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </div>
    </el-row>
    </div>
    <!-- 新增or编辑 -->
    <addAndEdit
      @refreshDataList="getDataList()"
      v-if="addVisible"
      ref="addNode"
      @closeDialog="closeDialog"
    />

    <!-- 评分等级" -->
    <scoreLe
      @refreshDataList="getDataList()"
      v-if="zhiShow == 3"
      v-on:fnBackOldPage="fnBackOldPage"
      ref="level"
      @closeDialog="closeDialog"
    />
  </basic-container>
</template>

<script>
    import assessMentHead from "@/views/statisticsanalysis/components/assessMentHead"; //转态头
    import selectSearch from "@/views/statisticsanalysis/components/selectSearch";
    import addAndEdit from "./addAndEdit"; //组件
    import scoreLe from "./scoreLe"; //组件
    // 接口 api
    import {
        assessList,
        startOrEndAssess,
        exportExcel,
        deleteAssess
    } from "@/api/statisticsanalysis/assessApi.js";
    export default {
        data() {
            return {
                canDelete :true,
                addVisible: false, //新增or编辑弹窗显隐
                searchheight: "0", //搜索组件的高度
                searchadvanced: true, //搜索的显隐
                contentName : '新增',
                searchForm: {}, //查询表单
                multipleSelection: [], //表格中被选中的数据
                dataList: [],
                dataListLoading: false,
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                rednermsg: true,
                zhiShow: "1",
            }
        },
        components: {
            assessMentHead, //状态头
            selectSearch,//下拉选搜索组件
            addAndEdit, //新增or编辑组件
            scoreLe

        },
        created() {
            this.getDataList();

        },
        methods: {
            //选择统计名称执行搜索功能
            getDataByTjmc(tjmc){
                this.searchForm.hzzqmc = tjmc ;
                this.getDataList() ;
            },
            // 查询条件的展开关闭
            searchchangeadvanced() {
                this.searchadvanced = !this.searchadvanced;
                if (this.searchadvanced) {
                    this.searchheight = "auto";
                }
            },
            dataView(data){
                this.zhiShow = 3;
                this.$nextTick(() => {
                    this.$refs.level.init(data);
                });

            },
            // 勾选数据
            handleSelectionChange(val) {
                let isDelete = val.every(it => {
                    return it.iskq == 0;
                });
                if (val.length == 0) {
                    this.canDelete = true;
                } else {

                    if (isDelete) {
                        this.canDelete = false;
                    } else {
                        this.canDelete = true;
                    }
                }
                this.multipleSelection = val;
            },
            // 新增或者编辑
            addOrEdit(data) {
                this.addVisible = true;
                if (data) {
                    this.$nextTick(() => {
                        this.$refs.addNode.init(data);
                    });
                } else {
                    this.$nextTick(() => {
                        this.$refs.addNode.init();
                    });
                }
            },
            //删除数据
            deleteBatch(){
                this.$confirm("此操作将删除选中的数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        let ids = this.multipleSelection.map(it => {
                            return it.id;
                        });
                        deleteAssess(ids.toString()).then(r => {
                            if (r.data.code == 0) {
                                this.$message.success("删除成功");
                                this.getDataList();
                            } else {
                                this.$message.error(r.data.msg);
                                this.getDataList();
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            workStart(data){
              startOrEndAssess(data.iskq, data.id)
                  .then(r => {
                      if (r.data.code == 0) {
                          this.$message.success("操作成功");
                          this.getDataList();
                      } else {
                          this.$message.error(r.data.msg);
                          this.getDataList();
                      }
                  })
                  .catch(err => {
                      this.getDataList();
                  });

            },
            download(){
                const rows = this.$refs.tableRef.selection;
                if (rows.length == 0) {
                    this.$message.closeAll();
                    this.$message({
                        type: "info",
                        message: "请至少选择一条数据提交!"
                    });
                    return false;
                }else {
                    if (rows.length == 1) {
                        let ids = this.$refs.tableRef.selection.map(it => {
                            return it.id;
                        });
                        console.log(ids)
                        const filename = "导出" + ".xlsx";

                        exportExcel(
                            Object.assign(
                                {
                                    ids: ids.toString()
                                }
                            )
                        ).then((response) => {
                            this.$message.closeAll();
                            this.$message.success("导出成功!");
                            let url = window.URL.createObjectURL(new Blob([response.data]));
                            let link = document.createElement("a");
                            link.style.display = "none";
                            link.href = url;
                            link.setAttribute("download", filename);
                            document.body.appendChild(link);
                            link.click();
                        });
                    }else{
                        this.$message.closeAll();
                        this.$message({
                            type: "info",
                            message: "至多只能选择一条数据提交!"
                        });
                        return false;
                    }

                }
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true;

                assessList(
                    Object.assign(
                        {
                            current: this.pageIndex, //当前页
                            size: this.pageSize, // 每页条数
                        },
                        this.searchForm,
                    )
                )
                    .then(response => {
                        this.dataList = response.data.data.records;
                        this.totalPage = response.data.data.total;
                        this.dataListLoading = false;
                    })
                    .catch(() => {
                        this.dataListLoading = false;
                    });
            },
            //子集信息
            fnBackOldPage(data) {
                if (data == "返回原页面") {
                    this.zhiShow = 1;
                }
            },
            indexMethod(index) { //修改table表索引

                return (this.pageIndex-1)*this.pageSize+index+1;
            },
            // 关闭新增or编辑弹窗
            closeDialog() {
                this.addVisible = false;
                this.getDataList();
            }
        }
    }
</script>

<style scoped>

</style>
